package com.web.base.log.provider;

import com.blue.jdbc.Page;
import com.blue.validation.ValidationUtil;
import com.blue.validation.group.SaveModel;
import com.blue.validation.group.UpdateModel;
import com.web.base.log.exception.AccusationException;
import com.web.base.log.facade.AccusationFacade;
import com.web.base.log.message.AccusationRequest;
import com.web.base.log.message.AccusationResponse;
import com.web.base.log.model.Accusation;
import com.web.base.log.service.AccusationService;
import com.web.base.log.util.AccusationConvertor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * 举报接口实现类
 * 
 * @author 代码由 blue-code 框架生成
 * @since 1.0 2018年06月20日
 */
@Component("logAccusationProvider")
public class AccusationProvider implements AccusationFacade
{
	@Autowired
	private AccusationService accusationService;
	
	public AccusationProvider()
	{
	}

	@Override
	public AccusationResponse add(AccusationRequest request)
	{
		ValidationUtil.valid(request, SaveModel.class);
		Accusation accusation = AccusationConvertor.toAccusation(request);
		accusation = accusationService.save(accusation);
		AccusationResponse response = AccusationConvertor.toResponse(accusation);
		return response;
	}

	@Override
	public AccusationResponse update(AccusationRequest request)
	{
		ValidationUtil.valid(request, UpdateModel.class);
		Accusation accusation = AccusationConvertor.toAccusation(request);
		accusation = accusationService.save(accusation);
		AccusationResponse response = AccusationConvertor.toResponse(accusation);
		return response;
	}

	@Override
	public AccusationResponse get(Integer id)
	{
		if (id == null)
			throw new AccusationException("Id不能为空");
			
		Accusation accusation = accusationService.get(id);
		AccusationResponse response = AccusationConvertor.toResponse(accusation);
		return response;
	}

	@Override
	public Page<AccusationResponse> listPage(AccusationRequest request, int start, int size)
	{
		Accusation accusation = AccusationConvertor.toAccusation(request);
		Page<Accusation> page = new Page<>(size);
		page.setCurPage(start);
		page = accusationService.list(accusation, page);
		Page<AccusationResponse> pageResponse = AccusationConvertor.toResponse(page);
		return pageResponse;
	}
	
}
